package com.example.botany_api.controller;

import com.alibaba.fastjson.JSON;
import com.example.botany_api.bean.Gallery;
import com.example.botany_api.bean.GalleryType;
import com.example.botany_api.bean.User;
import com.example.botany_api.service.GalleryService;
import com.example.botany_api.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 漫画忧作
 * @Date 2022/10/31 20:35
 * @Version 1.0
 */
@RestController
public class GalleryController {
    @Autowired
    private GalleryService galleryService;

    //    管理员 获得类别列表
    @RequestMapping("/admin/galleryTypeList")
    public String galleryTypeList(@RequestBody HashMap req) {
        String type_name = req.get("type_name").toString();
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", galleryService.getGalleryType(type_name, (current - 1) * size, size));
        res.put("total", galleryService.getGalleryTypeTotal(type_name));

        return JSON.toJSONString(res);
    }

    //    管理员 添加分类数据
    @RequestMapping("/admin/add_GalleryType")
    public String add_GalleryType(@RequestBody GalleryType galleryType) {
        int i = galleryService.add_GalleryType(galleryType);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    //    管理员 修改分类数据
    @RequestMapping("/admin/up_GalleryType")
    public String up_GalleryType(@RequestBody GalleryType galleryType) {
        System.out.println(galleryType);
        int i = galleryService.up_GalleryType(galleryType);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    //    管理员 删除数据
    @RequestMapping("/admin/dl_GalleryType")
    public String dl_GalleryType(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = galleryService.dl_GalleryType(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    @Autowired
    OssUtil ossUtil;

    //存储图片地址
    @RequestMapping("/admin/setGalleryImg")
    public String addImg(MultipartFile file) {
        //调用上传阿里云oss
        String str = ossUtil.checkImage(file);
        System.out.println(str);
        return str;
    }

    //    管理员 获得植物图库列表
    @RequestMapping("/admin/galleryList")
    public String galleryList(@RequestBody HashMap req) {
        String name = req.get("name").toString();
        int type_id;
        String address = req.get("address").toString();
        if (req.get("type_id").toString().equals("")) {
            type_id = 0;
        } else {
            type_id = Integer.parseInt(req.get("type_id").toString());
        }
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", galleryService.getGallery(name, address, type_id, (current - 1) * size, size));
        res.put("total", galleryService.getGalleryTotal(name, address, type_id));

        return JSON.toJSONString(res);
    }

    //    管理员 添加植物图库数据
    @RequestMapping("/admin/add_Gallery")
    public String add_Gallery(@RequestBody Gallery gallery) {
        int i = galleryService.add_Gallery(gallery);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    //    管理员 修改植物图库数据
    @RequestMapping("/admin/up_Gallery")
    public String up_Gallery(@RequestBody Gallery gallery) {
        System.out.println(gallery);
        int i = galleryService.up_Gallery(gallery);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    //    管理员 植物图库删除数据
    @RequestMapping("/admin/dl_Gallery")
    public String dl_Gallery(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = galleryService.dl_Gallery(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }

    //    管理员 所有分类类别
    @RequestMapping("/admin/optionGalleryList")
    public String optionList() {
        Map res = new HashMap();
        res.put("dataList", galleryService.getOptionList());

        return JSON.toJSONString(res);
    }

    //    推荐阅读
    @RequestMapping("/admin/galleryListCollect")
    public String galleryListCollect() {

        Map res = new HashMap();
        res.put("dataList", galleryService.getGalleryListCollect());

        return JSON.toJSONString(res);
    }

    //    植物图库数据详情 增加浏览量
    @RequestMapping("/admin/galleryAddViews")
    public String galleryAddCollect(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        Map res = new HashMap();
        res.put("dataList", galleryService.galleryAndType(id));
        galleryService.AddViews(id);
        return JSON.toJSONString(res);
    }

    //    添加植物图库收藏
    @RequestMapping("/galleryAddSave")
    public String galleryAddSave(@RequestBody HashMap req) {
        int g_id = Integer.parseInt(req.get("g_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        int i=galleryService.galleryAddSave(g_id, u_id);
        if(i>0){
            return "ok";
        }

        return "err";
    }

    //    判断有没有收藏
    @RequestMapping("/isGalleryAddSave")
    public String isGalleryAddSave(@RequestBody HashMap req) {
        int g_id = Integer.parseInt(req.get("g_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        Map res = new HashMap();
        int i = galleryService.isGalleryAddSave(g_id, u_id);
        res.put("isSave", i);
        return JSON.toJSONString(res);
    }
    //    取消收藏
    @RequestMapping("/galleryCancelSave")
    public String galleryCancelSave(@RequestBody HashMap req) {
        int g_id = Integer.parseInt(req.get("g_id").toString());
        int u_id = Integer.parseInt(req.get("u_id").toString());
        int i = galleryService.galleryCancelSave(g_id, u_id);
        if(i>0){
            return "ok";
        }

        return "err";
    }
    //    获得植物图库收藏数据
    @RequestMapping("/galleryListSave")
    public String galleryListSave(@RequestBody HashMap req) {
        int u_id = Integer.parseInt(req.get("u_id").toString());

        Map res = new HashMap();
        res.put("dataList", galleryService.galleryListSave(u_id));

        return JSON.toJSONString(res);
    }
    //    设置植物原产地
    @RequestMapping("/admin/selectMap")
    public String selectMap(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        String address = req.get("address").toString();
        String lng = req.get("lng").toString();
        String lat = req.get("lat").toString();
        int i=galleryService.selectMap(id,address,lng,lat);
        if(i>0){
            return "ok";
        }

        return "err";
    }

    //    获得周边植物原产地
    @RequestMapping("/getLatLngNameId")
    public String getLatLngNameId(@RequestBody HashMap req) {
        String lng = req.get("lng").toString();
        String lat = req.get("lat").toString();
        Map res = new HashMap();
        res.put("dataList", galleryService.getLatLngNameId(lng,lat));
        return JSON.toJSONString(res);
    }
}
